import java.util.ArrayList;
import java.util.List;

class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }


public class LeetCode {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode prev = head;
        int tmp = 0;
        while(l1!=null||l2!=null){
            if(l1!=null){
                tmp+=l1.val;
                l1 = l1.next;
            }
            if(l2!=null){
                tmp+=l2.val;
                l2 = l2.next;
            }
            prev = new ListNode(tmp%10);
            prev = prev.next;
            tmp/=10;
        }
        return head.next;
    }
}
